import {toPlainObjectDeep} from '../../../utils/toPlainObjectDeep';
import {observable, action} from 'mobx';
import {BuBean, BuSearchBean, BuController} from '../../../model/bu/Bu';
import _ from 'lodash';
import {FormStore, ListStore, FieldItem, isRequired, isSelectRequired, toJSDeep} from '../../common/store/Store';
import {message} from 'antd';

const buController = new BuController();

export class BuFields implements BuBean<FieldItem> {
    constructor(type = 'add'){
        if(type === 'edit'){
            _.forEach(this, (field:FieldItem, key: string) => {
                if(field.hasFeedback === true){
                    field.validateStatus = 'success';
                }
            })  
        }
    }

    id: FieldItem = new FieldItem({
        value: null
    });
    description: FieldItem = new FieldItem({
        value: ''
    });
    name: FieldItem = new FieldItem({
        value: '',
        hasFeedback: true,
        required: true
    });
    title: FieldItem = new FieldItem({
        value: '',
        hasFeedback: true,
        required: true
    });
}

export class BuSearchFields implements BuSearchBean<FieldItem>{
    description: FieldItem = new FieldItem({
        value: ''
    });
    title: FieldItem = new FieldItem({
        value: ''
    });
}

const addDefaultFields = toPlainObjectDeep(new BuFields());
const editDefaultFields = toPlainObjectDeep(new BuFields('edit'));

export class BuBaseFormStore extends FormStore<BuFields, BuBean<any>> {
    @observable confirmLoading:boolean = false;

    @action changeConfirmLoading(params){
        this.confirmLoading = params;
    }

    validators = {
        name: [isRequired],
        title:[isRequired]
    };
}

export class Add extends BuBaseFormStore {
    defaultFields = _.cloneDeep(addDefaultFields);

    @observable fields: BuFields = _.cloneDeep(addDefaultFields);

    @action addBu(params:BuBean<any>){
        return buController.addBu(params);
    }

    @action load(){
        this.show();
    }
}

export class Edit extends BuBaseFormStore {
    defaultFields = _.cloneDeep(editDefaultFields);

    @observable fields: BuFields = _.cloneDeep(editDefaultFields);

    @action load(id: number) {
        buController.getBu({id:id}).then((res) => {
            if(res.success){
                let data = res.result;
                this.bindData(data);
            }else{
                message.error(res.message,3);
            }
        })
    }

    @action editBu(params:BuBean<any>){
        return buController.editBu(params);
    }

    @action bindData(data){
        this.changeFields({
            title: { disabled:true },
            name: { disabled:true }
        });

        this.show(data);
    }
}

class Check extends BuBaseFormStore {
    @observable fields: BuFields = _.cloneDeep(addDefaultFields);
}

export class List extends ListStore<BuBean<any>> {
    @action searchBu(params?:any){
        this._SpiLoading = true;

        buController.searchBu(params).then((data) =>{
            this.searchCallback(data);
        })
    }

    @action delete(id: number){
       return buController.deleteBu({id:id});
    }

    @action load(params:any = {}){
        this.searchBu(params);
    }

    @action refresh(search: Search){
        this.changeState(true);
        search.clearAll();
        this.load();
    }
}

const searchDefaultFields = toPlainObjectDeep(new BuSearchFields());

class Search extends FormStore<BuSearchFields,BuSearchBean<any>>{
    
    defaultFields = _.cloneDeep(searchDefaultFields);

    @observable fields: BuSearchFields = _.cloneDeep(searchDefaultFields);

    @observable expand:boolean = false;

    @action OnExpand(expand){
        this.expand = !expand;
    }

    @action clearSearch(list:List){
        this.clearAll();
        list.load();
    }

    @action clearSearchState(){
        this.expand = false;
        this.clearAll();
    }
}

export const listStore = new List();
export const addStore = new Add();
export const editStore = new Edit();
export const checkStore = new Check();
export const searchStore = new Search();
